﻿using AppsModel.Data_Layer.Concrete;
using AppsModel.Data_Layer.Interface;
using AppsModel.Models;
using MVC_basic.Data_Layer.Interface;
using MVC_basic.Helpers;
using MVC_basic.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;
using WebMatrix.WebData;

namespace MVC_basic.Controllers
{
    public class HomeController : Controller
    {
        private IdbApp db;
        Stopwatch timer;
        public HomeController(IdbApp DIdb)
        {
            db = DIdb;
            WebSecurity_Singleton.init();
            timer = new Stopwatch();
            timer.Start();
        }
        [Authorize]
        public ActionResult Index()
        {
            //IdbWorkout dbW = new dbWorkout();
            //List<addWorkoutField> fields = new List<addWorkoutField>(){ new addWorkoutField(){ id = 1, fieldName = "set", fieldValue = "3" }, new addWorkoutField(){ id = 3, fieldName = "repitition", fieldValue = "15" }};
            //Workout work = dbW.addWorkout("Sit-Ups", 1, fields, 1);
            //Workout work = dbW.updateWorkout(1, "Sitt upps", 1, fields, 1);
            //Workout work = dbW.getWorkout(7);
            //dbW.deleteWorkout(7);
            List<App> apps = new List<App>();
            if (User.Identity.IsAuthenticated)
            {
                int userId = WebSecurity.GetUserId(User.Identity.Name);
                apps = db.getInstalledApps(userId);
            }
            timer.Stop();
            long elapsedTime = timer.ElapsedMilliseconds;
            ViewBag.elapsedTime = elapsedTime;
            return View(apps);
        }

        [Authorize]
        public ActionResult Install()
        {
            List<App> apps = db.getApps();
            return View(apps);
        }

        public bool uninstallApp(int AppId)
        {
            db.uninstallApp(AppId, 1);
            return true;
        }
    }
}
